AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
--include('entities/base_wire_entity/init.lua')
include( 'shared.lua' )
util.PrecacheSound( "SB/Artillery2.wav" )

function ENT:Initialize()

	self.Entity:SetModel( "models/Slyfo/artycannon.mdl" ) 
	self.Entity:SetName("Heavy Cannnon")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	if WireAddon then
		self.Inputs = WireLib.CreateInputs( self, { "Fire", "FireType" } )
	end

	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	self.Entity:SetKeyValue("rendercolor", "255 255 255")
	self.PhysObj = self.Entity:GetPhysicsObject()
	self.FireMode = 1
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16 + Vector(0,0,300)
	
	local ent = ents.Create( "weapon_HeavyCannon" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent.SPL = ply
	
	return ent
	
end

function ENT:TriggerInput(iname, value)		
	if (iname == "Fire") then
		if (value > 0) then
			self.Entity:HPFire()
		end
	elseif iname == "FireType" then
		if value > 1 then
			self.FireMode = value
		else
			self.FireMode = 1
		end
	end
end

function ENT:PhysicsUpdate()

end

function ENT:Think()
	--self.val1 = RD_GetResourceAmount(self.Entity, "Munitions")

end

function ENT:PhysicsCollide( data, physobj )
	
end

function ENT:OnTakeDamage( dmginfo )
	
end

function ENT:Use( activator, caller )

end

function ENT:HPFire()
	local CDown = self.Entity:GetNetworkedFloat("ReloadTime")
	if (CurTime() >= CDown) then
		local ammo = self:GetResourceAmount("Ammo Exp")
		local plasma = self:GetResourceAmount("Plasma")
		if (ammo >= 100 and self.FireMode == 1) then
			self:ConsumeResource("Ammo Exp", 50)
			local Size = "ammo_shell_large"
			local Pos = self.Entity:GetPos() + (self.Entity:GetUp() * 155) 
			local Ang = self.Entity:GetUp():Angle()
			local Vel = self.Entity:GetUp() * 10000
			local Col = Color(255,255,255,255)
			local Dam = 300
			self:SetShellType(Col,Dam)
			self:MakeShell(CCD,Size,Pos,Ang,Vel)
			self:Recoil(10000)
			self.Entity:SetNetworkedFloat("ReloadTime",CurTime() + 5)		
			local effectdata = EffectData()
			effectdata:SetOrigin(self.Entity:GetPos() +  self.Entity:GetUp() * 150)
			effectdata:SetStart(self.Entity:GetPos() +  self.Entity:GetUp() * 150)
			util.Effect( "Explosion", effectdata )
			self.Entity:EmitSound("SB/Artillery2.wav", 500)
		elseif(plasma>=10) then
			self:ConsumeResource("Plasma", 10)
			local Size = "ammo_shell_large"
			local Pos = self.Entity:GetPos() + (self.Entity:GetUp() * 155) 
			local Ang = self.Entity:GetUp():Angle()
			local Vel = self.Entity:GetUp() * 10000
			local Col = Color(255,0,255,255)
			local Dam = 600
			self:SetShellType(Col,Dam)
			self:MakeShell(CCD,Size,Pos,Ang,Vel)
			self:Recoil(10000)
			self.Entity:SetNetworkedFloat("ReloadTime",CurTime() + 5)		
			local effectdata = EffectData()
			effectdata:SetOrigin(self.Entity:GetPos() +  self.Entity:GetUp() * 150)
			effectdata:SetStart(self.Entity:GetPos() +  self.Entity:GetUp() * 150)
			util.Effect( "Explosion", effectdata )
			self.Entity:EmitSound("SB/Artillery2.wav", 500)
		end
	end
end
